// pages/goDiagnostic/goDiagnostic.js
const app = getApp();
const request = require('../../utils/request.js');
const util = require('../../utils/util.js');
const ApiRootUrl = app.globalData.REQUEST_IP_HEAD;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    apiRootUrl: ApiRootUrl,
    TabCur: 1,
    scrollLeft: 0,
    medicine: [],
    disease: [],
    diagnosticMessage: "", //诊断信息
    medicineIds: "", //药品单
    dosage: "", //药品用量
    useway: "", //药品用法
    hasMedicine: 0,
    hasDisease: 0,
    stuCode: "",
    check: '',
    disabled: true,
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  tabSelect(e) {
    console.log(e);
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },

  /**
   * 输入患者学号/工号
   * @param {*} e 
   */
  idInput(e) {
    this.setData({
      stuCode: e.detail.value.replace(/(^\s*)|(\s*$)/g, "")
    })
  },

  /**
   * 获取疾病信息
   */
  getDisease() {
    var that = this;
    request.get('/disease/findAllValid', {}).then((res) => {
      if (res.code == 200) {
        var diseaseList = res.data;
        for (var i = 0; i < diseaseList.length; i++) {
          diseaseList[i].isChosen = false;
          diseaseList[i].isShow = true;
        }
        that.setData({
          disease: diseaseList
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 选择疾病
   */
  chooseDisease(e) {
    var diseaseList = this.data.disease;
    diseaseList[e.currentTarget.dataset.index].isChosen = true;
    diseaseList[e.currentTarget.dataset.index].isShow = false;
    this.setData({
      disease: diseaseList,
      hasDisease: this.data.hasDisease + 1
    })
  },

  /**
   * 移除疾病
   */
  moveDisease(e) {
    var diseaseList = this.data.disease;
    diseaseList[e.currentTarget.dataset.index].isChosen = false;
    diseaseList[e.currentTarget.dataset.index].isShow = true;
    this.setData({
      disease: diseaseList,
      hasDisease: this.data.hasDisease - 1
    })
  },

  /**
   * 选择药物
   * @param {*} e 
   */
  chooseMedicine(e) {
    let list = this.data.medicine;
    let has = this.data.hasMedicine;
    if (list[e.currentTarget.dataset.index].isChosen == true) {
      list[e.currentTarget.dataset.index].isChosen = false;
      list[e.currentTarget.dataset.index].back = "#f5f5f5";
      list[e.currentTarget.dataset.index].color = "black";
      has--;
    } else {
      list[e.currentTarget.dataset.index].isChosen = true;
      list[e.currentTarget.dataset.index].back = "#EEF2FE";
      list[e.currentTarget.dataset.index].color = "#527CF5";
      has++;
    }
    this.setData({
      medicine: list,
      hasMedicine: has
    })
  },

  /**
   * 减药的数量
   * @param {*} e 
   */
  move(e) {
    let list = this.data.medicine;
    if (list[e.currentTarget.dataset.index].dosage != 0) {
      list[e.currentTarget.dataset.index].dosage--;
    }
    this.setData({
      medicine: list,
    })
  },

  /**
   * 加药的数量
   * @param {*} e 
   */
  add(e) {
    let list = this.data.medicine;
    list[e.currentTarget.dataset.index].dosage++;
    this.setData({
      medicine: list,
    })
  },

  /**
   * 取消选择药物
   * @param {*} e 
   */
  delete(e) {
    let list = this.data.medicine;
    list[e.currentTarget.dataset.index].isChosen = false;
    list[e.currentTarget.dataset.index].back = "#f5f5f5";
    list[e.currentTarget.dataset.index].color = "black";
    this.setData({
      medicine: list,
      hasMedicine: this.data.hasMedicine - 1
    })
  },

  /**
   * 修改药品信息
   * @param {*} e 
   */
  change(e) {
    let list = this.data.medicine;
    if (list[e.currentTarget.dataset.index].disable == true) {
      list[e.currentTarget.dataset.index].disable = false;
      list[e.currentTarget.dataset.index].border = "1px solid gray";
    } else {
      list[e.currentTarget.dataset.index].disable = true;
      list[e.currentTarget.dataset.index].border = "none";
    }
    this.setData({
      medicine: list,
    })
  },

  /**
   * 修改药物信息1
   * @param {*} e 
   */
  change1(e) {
    var list = this.data.medicine;
    list[e.currentTarget.dataset.index].useway[0] = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
    this.setData({
      medicine: list,
    })
  },
  /**
   * 修改药物信息2
   * @param {*} e 
   */
  change2(e) {
    var list = this.data.medicine;
    list[e.currentTarget.dataset.index].useway[1] = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
    this.setData({
      medicine: list,
    })
  },
  /**
   * 修改药物信息3
   * @param {*} e 
   */
  change3(e) {
    var list = this.data.medicine;
    list[e.currentTarget.dataset.index].useway[2] = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
    this.setData({
      medicine: list,
    })
  },

  /**
   * 搜索药物
   * @param {*} e 
   */
  searchMedicine(e) {
    let key = e.detail.value.replace(/(^\s*)|(\s*$)/g, "");
    let list = this.data.medicine;
    for (let i = 0; i < list.length; i++) {
      let a = key;
      let b = list[i].title;
      if (b.search(a) != -1) {
        list[i].isShow = true
      } else {
        list[i].isShow = false
      }
    }
    this.setData({
      medicine: list
    })
  },

  /**
   * 获取药品信息
   */
  getMedicine() {
    var that = this;
    request.get('/medicine/findAllValid', {}).then((res) => {
      if (res.code == 200) {
        var medicineList = res.data;
        for (var i = 0; i < medicineList.length; i++) {
          medicineList[i].useway = medicineList[i].useway.split('+');
          medicineList[i].isShow = true;
          medicineList[i].isChosen = false;
          medicineList[i].back = "#f5f5f5";
          medicineList[i].color = "black";
          medicineList[i].disable = true;
          medicineList[i].dosage = 0;
          medicineList[i].border = "none";
        }
        that.setData({
          medicine: medicineList
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 输入诊断信息
   */
  diagnosticInput(e) {
    this.setData({
      diagnosticMessage: e.detail.value.replace(/(^\s*)|(\s*$)/g, "")
    })
  },

  /**
   * 上传诊断信息
   */
  makeDiagnostic() {
    //签名必须同意上传
    if (this.data.check == false) {
      wx.showModal({
        content: "请同意上传签名"
      })
      return;
    }
    //患者学号/工号必须有
    if (this.data.stuCode == "") {
      wx.showModal({
        content: "请输入患者学号/工号"
      })
      return;
    }
    //主诊断疾病必须有
    var diseaseIds = "";
    if (this.data.hasDisease == 0) {
      wx.showModal({
        content: "请选择主诊断疾病信息"
      })
      return;
    } else {
      let diseaseList = this.data.disease;
      let flag = 0;
      for (var i = 0; i < diseaseList.length; i++) {
        if (flag == 0 && diseaseList[i].isChosen == true) {
          diseaseIds = diseaseList[i].id;
          flag = 1;
        } else if (flag == 1 && diseaseList[i].isChosen == true) {
          diseaseIds += "," + diseaseList[i].id;
        }
      }
    }
    //验证药物信息
    var medicines = this.data.medicine;
    var presbInformation;
    var usage;
    var dosage;
    var price = 0;
    if (this.data.hasMedicine > 0) {
      var flag = 0;
      for (var i = 0; i < medicines.length; i++) {
        if (medicines[i].isChosen == true) {
          price += medicines[i].dosage * medicines[i].price;
          if (medicines[i].dosage == 0) {
            wx.showModal({
              content: medicines[i].title + "的选择总数量为0"
            })
            return;
          }
          if (flag == 0) {
            flag = 1;
            presbInformation = "" + medicines[i].id;
            usage = "" + medicines[i].useway[0] + "+" + medicines[i].useway[1] + "+" + medicines[i].useway[2];
            dosage = "" + medicines[i].dosage;
          } else {
            presbInformation += "," + medicines[i].id;
            usage += "," + medicines[i].useway[0] + "+" + medicines[i].useway[1] + "+" + medicines[i].useway[2];
            dosage += "," + medicines[i].dosage;
          }
        }
      }
      this.setData({
        medicineIds: presbInformation,
        dosage: dosage,
        useway: usage
      })
    }
    //上传药物信息和诊断信息
    var that = this;
    if (this.data.diagnosticMessage == "") {
      wx.showModal({
        content: "请输入疾病诊断信息内容"
      })
    } else {
      request.post('/diagnosis/makeGoDiagnosis', {
        stuCode: that.data.stuCode,
        diagnosis: that.data.diagnosticMessage,
        symptoms: diseaseIds,
        medicines: that.data.medicineIds,
        dosage: that.data.dosage,
        useway: that.data.useway,
        price: price,
        drId: wx.getStorageSync('userInfo').id,
        sign: wx.getStorageSync('userInfo').sign
      }).then((res) => {
        if (res.code == 200) {
          that.hideModal();
          wx.showToast({
            title: '上传成功！',
            icon: 'success'
          })
          wx.navigateBack({
            delta: 1
          })
        } else {
          util.showToast(res.msg, 'none');
        }
      }).catch((errMsg) => {
        util.showToast(errMsg, 'error');
      })
    }
  },
  /**
   * 框选
   * @param {*} e 
   */
  checkboxChange: function (e) {
    // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    if (e.detail.value.length > 0) {
      this.setData({
        check: true,
        disabled: false
      });
    } else {
      this.setData({
        check: false,
        disabled: true
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMedicine();
    this.getDisease();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})